package com.bracelet.controller;

import com.bracelet.util.netty.NettySessionManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author  biany 20241129
 *  定时任务处理类
 * */
@Slf4j
@Controller
public class ScheduleController {

    @RequestMapping(value = "/heartrate")
    public String heartrate(@RequestBody String args){
        String returnMessage = "success";
        try {
            log.info("开始批量请求手环【心率】数据");
            sendMessage("hrtstart");
            log.info("批量请求手环【心率】数据【完成】！");
        }catch (Exception e){
            returnMessage = "fail";
            log.error(e.getMessage(),e);
        }
        return returnMessage;
    }

    @RequestMapping(value = "/bodytemperature")
    public String bodytemperature(@RequestBody String args){
        String returnMessage = "success";
        try {
            log.info("开始批量请求手环【体温】数据");
            sendMessage("wdstart");
        }catch (Exception e){
            returnMessage = "fail";
            log.error(e.getMessage(),e);
        }
        return returnMessage;
    }
    @RequestMapping(value = "/bloodpressure")
    public String bloodpressure(@RequestBody String args){
        String returnMessage = "success";
        try {
            log.info("开始批量请求手环【血压】数据");
            sendMessage("bldstart");
        }catch (Exception e){
            returnMessage = "bldstart";
            log.error(e.getMessage(),e);
        }
        return returnMessage;
    }
    @RequestMapping(value = "/bloodoxygen")
    public String bloodoxygen(@RequestBody String args){
        String returnMessage = "success";
        try {
            log.info("开始批量请求手环【血氧】数据");
            sendMessage("oxstart");
        }catch (Exception e){
            returnMessage = "fail";
            log.error(e.getMessage(),e);
        }
        return returnMessage;
    }




    /**
     * 向手环发送定时获取信息数据
     *[DW*设备ID*len*对应的数据,x]
     * x 为 1 则代表终端心率单次上传，上传完后自动关闭。
     * x 为 0 则代表终端心率上传关闭。
     * */
    private void sendMessage(String type){
        NettySessionManager.getSessionMap().forEach(
                (key,value) ->
                       value.getChannel().writeAndFlush("[DW*"+value.getBizKey()+"*0004*"+type+",1]")
        );
    }
}
